package leetcode;

/**
 * Created by shuowu on 2018/3/12.
 */
public class BinaryAlternate {

    public static void main(String[] args){
        BinaryAlternate object = new BinaryAlternate();
        System.out.println(object.hasAlternatingBits(Integer.MAX_VALUE / 2 + 1));
    }

    public boolean hasAlternatingBits(int n) {
        //由于是positive,所以我们首先获取一共是多少位
        int count = 0;
        if(n > (Integer.MAX_VALUE >> 1)) {
            count = Integer.SIZE - 1;
        } else {
            while ((1 << count) < n) {
                count++;
            }
        }
        System.out.println("the count is " + count);

        //判断count位是否都是10101，第一位肯定是1
        while (count > 1 && (((n >> (count - 1)) & 1) ^ ((n >> (count - 2)) & 1)) != 0) {
            count--;
        }
        if (count <= 1) {
            return true;
        } else {
            return false;
        }
    }
}
